import java.io.File;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Graphics;
import java.awt.image.*;
import javax.swing.ImageIcon;
import javax.imageio.*;

/*
 * Some code within this class used with permission from  Alexandre Cazes <alexandre.cazes@ebxtech.com>
 */

public class ImageUtils
{	
	// error handling function
    private static void error (String msg)
    {
        System.err.println (msg);
    }

	// this method returns true if the specified image has transparent pixels
	public static boolean hasAlpha(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage)
		{
            BufferedImage bimage = (BufferedImage)image;
            return bimage.getColorModel().hasAlpha();
        }
    
        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try
		{
            pg.grabPixels();
        }
		
		catch (InterruptedException e)
		{
			error ("Cannot load alpha information for Image: ");  
        }
    
        // Get the image's color model
        ColorModel cm = pg.getColorModel();
        return cm.hasAlpha();
    }
	
	// snippet from http://www.dreamincode.net/code/snippet1076.htm
	// converts an Image to a BufferedImage
	public static BufferedImage to_BufferedImage(Image image) {
        if (image instanceof BufferedImage) {return (BufferedImage)image;}
   
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();
   
        // Determine if the image has transparent pixels
        boolean hasAlpha = hasAlpha(image);
   
        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		
        try
		{
            // Determine the type of transparency of the new buffered image
            int transparency = (hasAlpha) ? Transparency.BITMASK : Transparency.OPAQUE;
   
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage (image.getWidth(null), image.getHeight(null), transparency);
        }
		
        catch (Exception e)
		{
			error ("Could not create BufferedImage.");
		}
   
        if (bimage == null)
		{
            // Create a buffered image using the default color model
            int type = (hasAlpha) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
			
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
   
        // Copy image to buffered image
        Graphics g = bimage.createGraphics();
   
        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();
   
        return bimage;
    }
	
	public static void save_image (Image img, File file)
    {
		BufferedImage bi = to_BufferedImage (img);
		
        String filetype = "png";

        try
        {
            ImageIO.write (bi, filetype, file);
        }

        catch (Exception e)
        {
            error ("Cannot save file: " + file.getName());
        }
    }
}